package cn.edu.hit.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import cn.edu.hit.javabean.*;
import cn.edu.hit.service.DictionaryService;
import cn.edu.hit.service.RoomBedService;
import cn.edu.hit.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private static final long serialVersionUID = 1L;
	private User user;
	private Student student;
	private static  ArrayList<Dictionary> genderList;
	private String roomBed;
	private String studentID;
	
	public String getStudentID() {
		return studentID;
	}
	public void setStudentID(String studentID) {
		this.studentID = studentID;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Student getStudent() {
		return student;
	}
    
	public String getRoomBed() {
		return roomBed;
	}
	public void setRoomBed(String roomBed) {
		this.roomBed = roomBed;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public void setGenderList(ArrayList<Dictionary> agenderList) {
		genderList = agenderList;
	}

	public ArrayList<Dictionary> getGenderList() {
		return genderList;
	}
	public UserAction(){
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		session = request.getSession();
		session.getServletContext();
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
		if(UserService.login(user.getUsername(), user.getPassword(), user.getIdentify()))
			return "loginsuccess";
		else
			return "error";
	}
	public String login() throws Exception{
		//System.out.println("login:"+user.getUsername()+user.getPassword());
		if(UserService.login(user.getUsername(), user.getPassword(), user.getIdentify()))
		{
			session.setAttribute("username", user.getUsername());
			if(user.getIdentify().equals("admin")){
				session.setAttribute("identify", "admin");
				return "adminlogin";
			}else{
				session.setAttribute("identify", "student");
				return "studentlogin";
			}
		}
		else
			return "loginfail";
	}
	public String register() throws Exception{
		//genderList = (ArrayList<Dictionary>)DictionaryService.getDictionaryByAttributeName(ConstInfo.AttributeName.GENDER);
		if(UserService.register(student)){
			session.setAttribute("username", student.getStudentNumber());
			session.setAttribute("realname", student.getStudentName());
			if(student.getOldPassword()!=null){
				return "modifysuccess";
			}
			return "registersuccess";
		}
		if(student.getOldPassword()!=null){
			return "modifyfail";
		}
		return "registerfail";
	}
	
	/**
	 * 修改密码
	 * @return 
	 * @throws Exception
	 */
	public String changePass() throws Exception {
		// TODO Auto-generated method stub
		String username = (String)session.getAttribute("username");
		if(UserService.changePass(username, user.getOldpassword(), user.getPassword())){
			return "modsuccess";
		}
		else{
			return "modfailed";
		}
	}
	
	public String addUser()throws Exception{
		if(user.getUsername().length()<0)return "addfailed";
		if(UserService.addUser(user)){
			return "addsuccess";
		}
		return "addfailed";
	}
	
    public String selectBed() throws Exception{
    	if(roomBed == null)
    		System.out.println("hijfijid");
    	session.setAttribute("roombed", roomBed.substring(0, roomBed.length()-1));
    	System.out.println(roomBed.substring(0, roomBed.length()-1));
    	int ret = RoomBedService.selectRoomBed(roomBed, studentID);
    	session.setAttribute("selectbedret", ""+ret);
    	session.setAttribute("type",""+RoomBedService.getTypeByRoomNumber(roomBed.substring(0, roomBed.length()-1)));
    	System.out.println(session.getAttribute("selectbedret"));
		if(ret ==RoomBedService.SELECT_SUCCESS)
		{
			return "selectBedSuccess";
			//this.session.setAttribute("isSelectedTrue", arg1)
		}
		else
		{
			return "selectBedFail";
		}
	}
	
}
